//入口 
// 样式引入 
import './css/common.css'
import 'babel-polyfill'


// import ("es6-promise").polyfil;
//相关组件引入 
import Vue from 'vue'
import routes from './router/index' // 引入路由配置
import store from './vuex/store' //vuex配置

//ekementui 和主题
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-default/index.css'
Vue.use(ElementUI)
    //设置为 false 以阻止 vue 在启动时生成生产提示 
Vue.config.productionTip = false
import vueSignature from "vue-signature"
Vue.use(vueSignature)
import VueResource from 'vue-resource';
Vue.use(VueResource)
    // Vue.http.options.emulateJSON = true;

// Vue.http.options.root = window.location.origin + "/api";
// Vue.http.options.root = "http://192.168.30.22/" + "/api";

Vue.http.options.root = "http://localhost:16866/api";
Vue.http.headers.common = {
    'Cache-Control': "no-cache",
    'If-Modified-Since': '0'
};
//token验证
Vue.http.interceptors.push((request, next) => {
    store.state.loading = true;
    if (sessionStorage.getItem("Authorization")) {
        request.headers.append("Authorization", "Basic " + sessionStorage.getItem("Authorization"))
    }
    //统一添加 api 版本号
    if (request.method == "GET") {
        //是否有参数\
        var url = request.url;
        if (url.indexOf("?") > 0) {
            request.url += "&api-version=1.0"
        } else {
            request.url += "?api-version=1.0"
        }
    } else {
        request.url += "?api-version=1.0"
    }

    next((response) => {
        store.state.loading = false;
        if (response.status == 401) {
            window.location.href = window.location.origin;
            return;
        }
        // console.log(response);
    });
});

//实例化vue 
const main = new Vue({
    router: routes,
    store
}).$mount('#app');

var time;
routes.beforeEach((to, from, next) => {
    //打开加载中
    store.state.loading = true;
    next();
    //检测网络是否畅通
    clearTimeout(time);
    time = setTimeout(function() {
        if (store.state.loading)
            main.$notify.error({
                title: '网络错误',
                message: '网络错误'
            });
    }, 3000);
});
routes.afterEach((to, from) => {
    //关闭加载中
    store.state.loading = false;
});